package com.itheima.reggie.config;

import com.itheima.reggie.Listenerr.LoginCheck;

import com.itheima.reggie.Listenerr.UserCheck;
import com.itheima.reggie.exception.JacksonObjectMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;

import java.util.List;

@Configuration
public class LoginCofig extends WebMvcConfigurationSupport {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration interceptorRegistration =registry.addInterceptor(new LoginCheck());
      InterceptorRegistration interceptor =registry.addInterceptor(new UserCheck());
        String urls[]=new String[]
                {
                        "/employee/login",
                        "/employee/logout",
                        "/backend/**",
                        "/front/**",
                        "/common/**",
                        "/user/sendMsg",
                        "/user/login"
                };
        interceptorRegistration.addPathPatterns("/**").excludePathPatterns(urls );
       interceptor.addPathPatterns("/**").excludePathPatterns(urls);

    }
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        //  System.out.println("开始进行静态自学映射");
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
        registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");

    }    /* 设置静态资源映射:因为springboot里默认的是stataic和Thymelate里下的页面*/

    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        System.out.println("消息转化器");
        //新建一个自己的消息转化器,不使用框架默认的 如@RequestBody使用的就是框架默认的
        MappingJackson2HttpMessageConverter messageConverter=new MappingJackson2HttpMessageConverter();
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        //将自己添加的消息转化器追加到mvc框架中消息转化器集合中,0是代表第一个，让消息转化器第一个使用我们定义的
        converters.add(0,messageConverter);

    }

}
